#include <cstdio>
#define INF 1000000000

int main( )
{
    int f[ 26 ][ 26 ], i, j, k, n, m;
    char str[ 5 ];
    while ( scanf("%d%d", &n, &m) )
    {
        if ( n == 0 && m == 0 ) break;
        for ( i = 0; i < 26; i++ )
            for ( j = 0; j < 26; j++ )
                f[ i ][ j ] = INF;
        for ( i = 0; i < n; i++ )
        {
            scanf("%s", str);
            if ( str[ 1 ] != '-' )
                f[ str[ 0 ] - 'A' ][ str[ 1 ] - 'A' ] = 1;
            if ( str[ 2 ] != '-' )
                f[ str[ 0 ] - 'A' ][ str[ 2 ] - 'A' ] = 1;
        }
        for ( k = 0; k < 26; k++ )
            for ( i = 0; i < 26; i++ )
                for ( j = 0; j < 26; j++ )
                    if ( f[ i ][ j ] > f[ i ][ k ] + f[ k ][ j ] )
                        f[ i ][ j ] = f[ i ][ k ] + f[ k ][ j ];
        for ( i = 0; i < m; i++ )
        {
            scanf("%s", str);
            if ( f[ str[ 0 ] - 'A' ][ str[ 1 ] - 'A' ] != INF )
            {
                if ( f[ str[ 0 ] - 'A' ][ str[ 1 ] - 'A' ] == 1 )
                    puts("child");
                else
                {
                    for ( j = 2; j < f[ str[ 0 ] - 'A' ][ str[ 1 ] - 'A' ]; j++ )
                        printf("great-");
                    puts("grandchild");
                }
            }
            else if ( f[ str[ 1 ] - 'A' ][ str[ 0 ] - 'A' ] != INF )
            {
                if ( f[ str[ 1 ] - 'A' ][ str[ 0 ] - 'A' ] == 1 )
                    puts("parent");
                else
                {
                    for ( j = 2; j < f[ str[ 1 ] - 'A' ][ str[ 0 ] - 'A' ]; j++ )
                        printf("great-");
                    puts("grandparent");
                }
            }
            else
                puts("-");
        }
    }
    return 0;
}
